<?php

namespace Home\Controller;

class MemberController extends CommonController {

    public function index() {
        $user = M("user")->field("email,money,phone,avatar,qq,sina,renren")->where("id = " . session('userid') . "")->find();
        $signnum = M("sign")->where("uid = " . session('userid') . "")->count();
        $this->assign("user", $user);
        $this->assign("signnum", $signnum);
        $this->assign("ctitle", "用户中心");
        session('returnUrl', "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . ""); // 第三方登陆返回地址
        $this->display();
    }

    public function addr() {
        $lists = M("addr")->where("uid = " . session('userid') . "")->order("id DESC")->select();
        $this->assign("lists", $lists);
        $this->assign("ctitle", "地址管理");
        $this->display();
    }

    public function pwd() {
        $this->assign("ctitle", "修改密码");
        $this->display();
    }

    public function pwd_post() {
        $pwdOld = I('post.pwdOld');
        $info = M("user")->field("id")->where("id = '" . session('userid') . "' AND pwd = '" . md5($pwdOld) . "'")->find();
        if ($info) {
            $data['pwd'] = md5(I('post.pwd'));
            M("user")->where("id = '" . session('userid') . "'")->save($data);
            $this->success('修改密码成功！', U('Member/pwd'));
        } else {
            $this->error('修改失败！', U('Member/pwd'));
        }
    }

    public function messages() {
        $sql = "pid = 0 AND uid = " . session("userid") . "";
        $count = M('messages')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, 10);
        $lists = M('messages')->field("id,uid,content,addtime")->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order("id desc")->select();
        foreach ($lists as $k => $v) {
            $lists[$k]['sub'] = M('messages')->field("id,uid,uid2,content,addtime")->where("pid = " . $v['id'] . "")->order("id desc")->select();
        }
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("ctitle", "餐厅留言");
        $this->display();
    }

    public function questions() {
        $sql = "pid = 0 AND uid = " . session("userid") . "";
        $count = M('questions')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, 10);
        $lists = M('questions')->field("id,uid,content,addtime")->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order("id desc")->select();
        foreach ($lists as $k => $v) {
            $lists[$k]['sub'] = M('questions')->field("id,uid,uid2,content,addtime")->where("pid = " . $v['id'] . "")->order("id desc")->select();
        }
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("ctitle", "网站反馈");
        $this->display();
    }

    public function sign() {
        $first = strtotime(date("Y-m-01 00:00:00"));
        $first = date("w", $first);
        $weekArr = array("日", "一", "二", "三", "四", "五", "六");
        $maxDay = date('t', strtotime("" . date("Y") . "-" . date("m") . ""));
        for ($j = 0; $j < $first; $j++) {
            $blankArr[] = $j;
        }

        for ($i = 0; $i < $maxDay; $i++) {
            $z = $first + $i;
            $days[] = array("key" => $i, "key2" => $z % 7);
        }
        // print_r($days);
        $nowtime = date("Y年m月d日 H:i:s") . "&nbsp;&nbsp;星期" . $weekArr[date("w")];
        $this->assign("nowtime", $nowtime);
        $this->assign("days", $days);
        $this->assign("first", $first);
        $this->assign("blankArr", $blankArr);
        $total = $first + count($days);
        for ($x = 0; $x < 35 - $total; $x++) {
            $other[] = $x;
        }
        $this->assign("other", $other);

        $this->assign("ctitle", "会员签到");
        $this->display();
    }

    public function announce() {
        $sql = "pid = 4";
        $count = M('article')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, 10);
        $lists = M('article')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order("id desc")->select();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("ctitle", "系统公告");
        $this->display();
    }

    public function points() {
        $month_first = date("Y-m-01 00:00:00");
        $starttime = strtotime(I('get.starttime', $month_first));
        $endtime = strtotime(I('get.endtime', date("Y-m-" . date("t") . "")));
        $this->assign("starttime", date("Y-m-d", $starttime));
        $this->assign("endtime", date("Y-m-d", $endtime));
        $mtype = I('get.mtype', '', 'int');
        $sql = "1=1 AND uid = " . session('userid') . "";
        $count = M("log")->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, 10);
        $lists = M("log")->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order("id desc")->select();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("mtype", $mtype);
        $user = M("user")->field("money")->where("id = " . session('userid') . "")->find();
        $this->assign("user", $user);
        $this->assign("ctitle", '我的积分');
        $this->display();
    }

    public function order() {
        $count = M('order')->where("uid = " . session("userid") . "")->count();    //计算总数
        $Page = new \Think\Page($count, 10);
        $lists = M('order')->where("uid = " . session("userid") . "")->limit($Page->firstRow . ',' . $Page->listRows)->order("id desc")->select();
        foreach ($lists as $k => $v) {
            $lists[$k]['shop'] = M("shop")->field("name,phone")->where("id = " . $v['sid'] . "")->find();
            $lists[$k]['sub'] = M("order_goods")->where("pid = " . $v['id'] . "")->select();
            $lists[$k]['sendtime'] = getSingleField($v['sid'], 'shop', 'sendtime');
        }
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("ctitle", "我的订单");
        $this->display();
    }

    public function comments() {
        $sql = "uid = " . session("userid") . "";
        $count = M('comment')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, 10);
        $lists = M('comment')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order("addtime desc")->select();
        $str_goods = '';
        foreach ($lists as $k => $v) {
            $sub = M("order_goods")->field("gname,gnum")->where("pid = " . $v['orderid'] . "")->select();
            $num = count($sub);
            foreach ($sub as $k2 => $v2) {
                if ($num != $k2 + 1) {
                    $dunhao = "、";
                } else {
                    $dunhao = "";
                }
                $str_goods .= $sub[$k2]['gname'] . $sub[$k2]['gnum'] . "份" . $dunhao . "";
            }
            $lists[$k]['goods'] = $str_goods;
        }
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("ctitle", "订单评价");
        $this->display();
    }

}
